// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:28:23 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   FacturasPanel.java

package com.luxsoft.sw3.impap.ui.consultas;

import java.util.List;

import javax.swing.Action;

import com.luxsoft.siipap.cxc.ui.consultas.FacturaForm;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.swing.browser.FilteredBrowserPanel;
import com.luxsoft.siipap.swing.utils.MessageUtils;
import com.luxsoft.siipap.ventas.model.Venta;
import com.luxsoft.sw3.cfd.model.ComprobanteFiscal;

public class FacturasPanel extends FilteredBrowserPanel<Venta>
{
    public static class CancelablePredicate
        implements com.luxsoft.siipap.swing.browser.FilteredBrowserPanel.Predicate
    {

        public boolean evaluate(Object bean)
        {
            Venta v = (Venta)bean;
            if(v != null)
                return !v.isCancelado();
            else
                return false;
        }

        public CancelablePredicate()
        {
        }
    }

    public static class ImprimiblePredicate
        implements com.luxsoft.siipap.swing.browser.FilteredBrowserPanel.Predicate
    {

        public boolean evaluate(Object bean)
        {
            Venta v = (Venta)bean;
            if(v != null)
                return v.getImpreso() == null;
            else
                return false;
        }

        public ImprimiblePredicate()
        {
        }
    }


    public FacturasPanel()
    {
        super(Venta.class);
    }

    protected void init()
    {
        String props[] = {
            "sucursal.nombre", "origen", "pedido.entrega", "pedido.folio", "documento", "numeroFiscal", "fecha", "clave", "nombre", "total", 
            "pagos", "saldoCalculado", "facturista", "cancelado", "pedimento", "pedimentoSat"
        };
        String names[] = {
            "Suc", "Ori", "Entrega", "Pedido", "Docto", "N.Fiscal", "Fecha", "Cliente", "Nombre", "Total", 
            "Pagos", "Saldo", "Facturista", "Cancelado", "Pedimento", "Pedimento (SAT)"
        };
        addProperty(props);
        addLabels(names);
        installTextComponentMatcherEditor("Origen", new String[] {
            "origen"
        });
        installTextComponentMatcherEditor("Sucursal", new String[] {
            "sucursal"
        });
        installTextComponentMatcherEditor("Documento", new String[] {
            "documento"
        });
        installTextComponentMatcherEditor("N.Fiscal", new String[] {
            "numeroFiscal"
        });
        installTextComponentMatcherEditor("Cliente", new String[] {
            "nombre", "clave"
        });
        installTextComponentMatcherEditor("Total", new String[] {
            "total"
        });
        manejarPeriodo();
    }

    protected List<Venta> findData(){
    	
        return ServiceLocator2.getHibernateTemplate()
        	.find("from Venta v left join fetch v.pedido p " +
        			" where date(v.fecha) between ? and ? "
        			, new Object[] {
            periodo.getFechaInicial()
            , periodo.getFechaFinal()
        });
    	
    }

    protected void doSelect(Object bean)
    {
        Venta venta = (Venta)bean;
        FacturaForm.show(venta.getId());
    }

    public Action getCancelAction()
    {
        if(cancelAction == null)
        {
            cancelAction = addRoleBasedContextAction(new CancelablePredicate(), null, this, "cancelar", "Cancelar");
            cancelAction.putValue("SmallIcon", getIconFromResource("images/edit/delete_edit.gif"));
        }
        return cancelAction;
    }

    public Action getImprimirAction()
    {
        if(imprimirAction == null)
        {
            imprimirAction = addRoleBasedContextAction(new ImprimiblePredicate(), null, this, "imprimir", "Imprimir");
            imprimirAction.putValue("SmallIcon", getIconFromResource("images/file/printview_tsk.gif"));
        }
        return imprimirAction;
    }

    public void cancelar()
    {
        if(getSelectedObject() == null)
            return;
        Venta factura = (Venta)getSelectedObject();
        com.luxsoft.sw3.ventas.Pedido pedido = factura.getPedido();
        if(factura.isCancelado())
            return;
        if(MessageUtils.showConfirmationMessage((new StringBuilder("Cancelar factura: ")).append(factura.getDocumento()).toString(), "Cancelaciones"))
        {
            int index = source.indexOf(factura);
            if(index != -1)
            {
                java.util.Date fecha = ServiceLocator2.obtenerFechaDelSistema();
                factura = ServiceLocator2.getFacturasManager().cancelarFactura(factura.getId(), fecha);
                source.set(index, factura);
                selectionModel.clearSelection();
            }
        }
    }
    /*
    public void load(){
    	//cambiarPeriodo();
    	super.load();
    }
    */

    public void imprimir()
    {
        if(getSelectedObject() == null)
        {
            return;
        } else
        {
            Venta factura = (Venta)getSelectedObject();
            factura.getImpreso();
            return;
        }
    }

    public Action getGenerarCFDAction()
    {
        if(generarCFDAction == null)
        {
            generarCFDAction = addRoleBasedContextAction(null, null, this, "generarCFD", "Generar CFD");
            generarCFDAction.putValue("SmallIcon", getIconFromResource("images/file/printview_tsk.gif"));
        }
        return generarCFDAction;
    }

    public Action getImprimirCFDAction()
    {
        if(imprimirCFDAction == null)
        {
            imprimirCFDAction = addRoleBasedContextAction(null, null, this, "imprimirCFD", "Imprimir CFD");
            imprimirCFDAction.putValue("SmallIcon", getIconFromResource("images/file/printview_tsk.gif"));
        }
        return imprimirCFDAction;
    }

    public void generarCFD()
    {
        Venta venta = (Venta)getSelectedObject();
        if(venta != null)
        {
            ComprobanteFiscal cfd = ServiceLocator2.getCFDManager().cargarComprobante(venta);
            logger.info((new StringBuilder("Factura digital generada: ")).append(cfd).toString());
        }
    }

    public void imprimirCFD(Venta venta1, ComprobanteFiscal comprobantefiscal)
    {
    }

    private Action cancelAction;
    private Action imprimirAction;
    private Action generarCFDAction;
    private Action imprimirCFDAction;
    
    public static void main(String[] args) {
		ServiceLocator2.getHibernateTemplate().find("from Venta v");
	}
}
